﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DisplayDemo
{
    internal class Manager
    {
        public static string IniPath { get; set; }

        public int Register(string guid)
        {
            const string url = "http://passport.kkcai.net/api.aspx?cmd=r&guid={0}";

            string code = new WebUtils().DoGet(String.Format(url, guid));

            if (String.IsNullOrEmpty(code))
            {
                return 99;
            }

            if (code == "1")
            {
                string key = CryptHelper.EnCryptDES(HardwareHelper.GetKey());
                
                IniFileHelper.IniWriteValue("license", "key", key, IniPath);
                
                IniFileHelper.IniWriteValue("license", "guid", guid, IniPath);
            }

            return TypeConvert.ToInt32(code);
        }

        public bool Valid()
        {
            string key = IniFileHelper.IniReadValue("license", "key", IniPath);

            if (String.IsNullOrEmpty(key))
                return false;

            //检查注册的时候绑定的机器
            if (CryptHelper.DeCryptDES(key) != HardwareHelper.GetKey())
                return false;

            //检查是否过期
            string guid = IniFileHelper.IniReadValue("license", "guid", IniPath);
            const string url = "http://passport.kkcai.net/api.aspx?cmd=v&guid={0}";

            string resp = new WebUtils().DoGet(String.Format(url, guid));

            if (String.IsNullOrEmpty(resp))
            {
                return false;
            }

            return resp != "0";
        }

        public void WriteConfig(string cfg)
        {
            IniFileHelper.IniWriteValue("config", "DS", cfg.Substring(0, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "DX", cfg.Substring(1, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "RowLine", cfg.Substring(2, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "Area", cfg.Substring(3, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "Color", cfg.Substring(4, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "Omit", cfg.Substring(5, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "CH", cfg.Substring(6, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "Border", cfg.Substring(7, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "Time", cfg.Substring(8, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "Font", cfg.Substring(9, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "Mod", cfg.Substring(10, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "LM", cfg.Substring(11, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "TW", cfg.Substring(12, 1), IniPath);
            IniFileHelper.IniWriteValue("config", "IsTime", cfg.Substring(13, 1), IniPath);
        }

        public string ReadConfig()
        {
            string value = String.Empty;

            value += IniFileHelper.IniReadValue("config", "DS", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "DX", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "CH", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "RowLine", "0", IniPath);

            value += IniFileHelper.IniReadValue("config", "Area", "3", IniPath);
            value += IniFileHelper.IniReadValue("config", "Color", "2", IniPath);

            value += IniFileHelper.IniReadValue("config", "Omit", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "Border", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "Time", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "Font", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "Mod", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "LM", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "TW", "0", IniPath);
            value += IniFileHelper.IniReadValue("config", "IsTime", "1", IniPath);

            return value;
        }

        public void ChangeScreen(string direct)
        {
            Screen scr = Screen.AllScreens[0];
            if (Screen.AllScreens.Length > 1)
            {
                foreach (Screen item in Screen.AllScreens)
                {
                    if (!item.Primary)
                    {
                        scr = item;
                        break;
                    }
                }
            }

            DEVMODE dm = DisplayHeler.CreateDevmode();

            DisplayHeler.EnumDisplaySettings(scr.DeviceName, -1, ref dm);

            int temp = dm.dmPelsHeight;
            dm.dmPelsHeight = dm.dmPelsWidth;
            dm.dmPelsWidth = temp;

            int index = dm.dmDisplayOrientation;

            if (direct == "left")
                dm.dmDisplayOrientation = (index == 3) ? 0 : index + 1;
            else
                dm.dmDisplayOrientation = (index == 0) ? 3 : index - 1;


            DisplayHeler.ChangeDisplaySettingsEx(scr.DeviceName, ref dm, null, 0, "");
        }
    }
}
